package com.huiminpay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.RandomUuidUtil;
import com.huiminpay.merchant.api.AppService;
import com.huiminpay.merchant.convert.AppCovert;
import com.huiminpay.merchant.dto.AppDTO;
import com.huiminpay.merchant.entity.App;
import com.huiminpay.merchant.entity.Merchant;
import com.huiminpay.merchant.mapper.AppMapper;
import com.huiminpay.merchant.mapper.MerchantMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 有点儿意思
 * @Description:
 */
@Service
public class AppServiceImpl implements AppService {


    @Autowired
    AppMapper appMapper;
    @Autowired
    MerchantMapper merchantMapper;

    /**
     * 创建商户应用
     *
     * @param merchantId 商户id
     * @param appDTO     商户信息
     * @return 商户信息
     * @throws BusinessException
     */
    @Override
    public AppDTO createApp(Long merchantId, AppDTO appDTO) throws BusinessException {
        //1.校验参数
        if (merchantId == null || appDTO == null){
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        //2.应用名不能重复
        Integer count = appMapper.selectCount(new LambdaQueryWrapper<App>().eq(App::getAppName, appDTO.getAppName()));
        if (count > 0){
            throw new BusinessException(CommonErrorCode.E_200004);
        }
        //3.商户是否通过资质申请
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (merchant == null){
            throw new BusinessException(CommonErrorCode.E_200002);
        }
        if (!"2".equals(merchant.getAuditStatus())) {
            throw new BusinessException(CommonErrorCode.E_200003);
        }
        //4.插入商户数据
        //4.1 dto转实体
        App app = AppCovert.INSTANCE.dto2entity(appDTO);
        app.setAppId(RandomUuidUtil.getUUID());
        app.setMerchantId(merchantId);
        appMapper.insert(app);
        //5.返回数据
        return AppCovert.INSTANCE.entity2dto(app);
    }

    /**
     * 查询商户下的应用列表
     * @param merchantId 商户id
     * @return 应用列表数据
     * @throws BusinessException
     */
    @Override
    public List<AppDTO> queryByMerchantId(Long merchantId) throws BusinessException {
        List<App> apps = appMapper.selectList(new LambdaQueryWrapper<App>().eq(App::getMerchantId, merchantId));
        return AppCovert.INSTANCE.listEntity2dto(apps);
    }

    /**
     * 查询应用的详情数据
     *
     * @param appId 应用id
     * @return 应用详情数据
     * @throws BusinessException
     */
    @Override
    public AppDTO queryAppDetail(String appId) throws BusinessException {
        App app = appMapper.selectOne(new LambdaQueryWrapper<App>().eq(App::getAppId, appId));
        return AppCovert.INSTANCE.entity2dto(app);
    }

    /**
     * 校验应用是否属于商户
     *
     * @param appId      应用id
     * @param merchantId 商户id
     * @return 应用属于商户
     */
    @Override
    public Boolean queryAppInMerchant(String appId, Long merchantId) {
        Integer count = appMapper.selectCount(new LambdaQueryWrapper<App>().eq(App::getAppId, appId)
                .eq(App::getMerchantId, merchantId));
        return count > 0;
    }
}
